//
//  TranslateView.swift
//  Chatup
//
//  Created by iron man on 2024/1/15.
//

import SwiftUI


struct TranslateView: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var imaginationText: String = ""
    @State private var local: String = "English"
    @State private var showLanguageView: Bool = false
    
    var body: some View {
        VStack {
            Spacer().frame(height: 16)
            HStack {
                Button {
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Image("nav_close")
                        .resizable()
                        .frame(width: 12, height: 12)
                }
                Spacer()
                // TODO: 判断是否是chatgpt4
//                Button {
//                    // TODO: 升级
//                } label: {
//                    Image("icon_setting_vip_right")
//                        .resizable()
//                        .frame(width: 77, height: 24)
//                }

            }.padding(.horizontal, 22)
                
            Spacer().frame(height: 16)
                
            Text("✍️ "+String.i18nText("feature_translator"))
                .font(.system(size: 28).weight(.bold))
                .foregroundColor(.white)
            Text(String.i18nText("feature_translate_hint1"))
                .font(.system(size: 16))
                .foregroundColor(Color(red: 0.51, green: 0.51, blue: 0.51))
                .frame(width: 316, height: 71, alignment: .topLeading)
                
            Spacer().frame(height: 26)
            Button {
                showLanguageView.toggle()
//                LanguageListView
            } label: {
                HStack{
                    Spacer().frame(width: 22)
                    Text(local)
                        .font(.system(size: 18))
                      .foregroundColor(Color(red: 0.61, green: 0.61, blue: 0.61))
                      .frame(width: 221, height: 28, alignment: .topLeading)
                      .padding(.vertical, 16)
                        
                    Spacer()
                    Image("ic_translate_bottom")
                    .resizable()
                    .frame(width: 11, height: 13)
                    
                    Spacer().frame(width: 22)
                    
                }
                .background(Color(red: 0.13, green: 0.13, blue: 0.14))
                .cornerRadius(10)
                .padding(.horizontal, 22)
            }

            
            Spacer().frame(height: 20)
                
            TextView(hint: String.i18nText("feature_translate_hint2"), text: $imaginationText).padding(.horizontal,23)
                
            Spacer()
            
            Button(action: {
                TranslateMessageBuilder().buildMessage(resouse: local+"#"+imaginationText, message: {
                    msg in
                    guard let message = msg else {
                        presentationMode.wrappedValue.dismiss()
                        return
                    }
                    
                    let msgModel = MsgModel(type: .user, messageType: .text, message: message, roleId: 0, isLike: false, isUnlike: false)
                    
                    
                    DispatchQueue.main.async {
                        NotificationCenter.default.post(name: Notification.Name("kAppendMessage"), object: ["feature":FeaturesType.translate,"msgs":[msgModel]])
                        presentationMode.wrappedValue.dismiss()
                    }
                })
            }) {
                Text(String.i18nText("feature_translator"))
                    .font(.system(size: 18).weight(.bold))
                    .foregroundColor(Color(red: 1, green: 1, blue: 1))
                    .foregroundColor(.clear)
                    .frame(minWidth: 0, maxWidth: .infinity)
                    .frame(height: 57)
                    .background(
                        imaginationText.isEmpty ? Color(red: 0.54, green: 0.54, blue: 0.55):
                        Color(red: 0.15, green: 0.75, blue: 0.46)
                    )
                    .cornerRadius(12)
            }
            .padding()

            Spacer().frame(height: 32)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.black.opacity(0.8))
        .edgesIgnoringSafeArea(.all)
        .sheet(isPresented: $showLanguageView) {
            LanguageListView(local: $local)
        }
        .onTapGesture {
            NotificationCenter.default.post(name: Notification.Name("kChangeFocused"), object: nil)
        }
    }
}

